{% extends "base.html" %}

{% block title %}护理员信息 - 长者照护计划系统{% endblock %}

{% block page_title %}护理员信息{% endblock %}

{% block extra_css %}
<style>
    .filter-section {
        margin-bottom: 20px;
        padding: 15px;
        border: 1px solid #dee2e6;
        border-radius: 5px;
        background-color: #f8f9fa;
    }
    
    .caregiver-table {
        font-size: 0.9rem;
    }
    
    .caregiver-table th, .caregiver-table td {
        padding: 0.5rem;
    }
    
    .caregiver-row:nth-child(odd) {
        background-color: rgba(0, 0, 0, 0.05);
    }
    
    .caregiver-row:hover {
        background-color: rgba(0, 123, 255, 0.1);
    }
    
    .json-display {
        background-color: #f8f9fa;
        border: 1px solid #dee2e6;
        border-radius: 4px;
        padding: 10px;
        font-family: monospace;
        white-space: pre-wrap;
        max-height: 300px;
        overflow-y: auto;
    }
    
    #skillsModal .modal-dialog {
        max-width: 90%;
    }
</style>
{% endblock %}

{% block content %}
<div class="row">
    <div class="col-md-12">
        <div class="card">
            <div class="card-header">
                <h5 class="card-title">护理员信息</h5>
            </div>
            <div class="card-body">
                <div class="filter-section">
                    <form id="caregiverFilterForm">
                        <div class="row">
                            <div class="col-md-6">
                                <label for="caregiver" class="form-label">护理员</label>
                                <input type="text" class="form-control" id="caregiver" name="caregiver" placeholder="输入护理员ID或姓名">
                            </div>
                            <div class="col-md-6">
                                <label class="form-label">&nbsp;</label>
                                <div class="d-grid">
                                    <button type="button" id="filterButton" class="btn btn-primary">筛选</button>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
                
                <div class="table-responsive">
                    <table class="table table-bordered caregiver-table">
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>护理员</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody id="caregiverTableBody">
                            {% for caregiver in caregivers %}
                            <tr class="caregiver-row" data-caregiver="{{ caregiver.attendant_name }}">
                                <td>{{ caregiver.id }}</td>
                                <td>{{ caregiver.attendant_name }}</td>
                                <td>
                                    <button class="btn btn-sm btn-info view-skills-btn" data-id="{{ caregiver.id }}" data-skills="{{ caregiver.skills }}">查看技能</button>
                                    <button class="btn btn-sm btn-success view-schedule-btn" data-id="{{ caregiver.id }}" data-caregiver="{{ caregiver.attendant_name }}">查看排班</button>
                                </td>
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
                
                <div class="d-flex justify-content-between align-items-center mt-3">
                    <div>
                        <span>显示 <span id="visibleCount">{{ caregivers|length }}</span> 条记录，共 <span id="totalCount">{{ caregivers|length }}</span> 条记录</span>
                    </div>
                    <div>
                        <button id="exportButton" class="btn btn-success">
                            <i class="fas fa-file-excel"></i> 导出到Excel
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- 护理员技能模态框 -->
<div class="modal fade" id="skillsModal" tabindex="-1" aria-labelledby="skillsModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="skillsModalLabel">护理员技能</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <div id="skillsContent" class="json-display"></div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">关闭</button>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script>
    $(document).ready(function() {
        // 记录总数
        const totalCount = $('.caregiver-row').length;
        $('#totalCount').text(totalCount);
        
        // 筛选按钮点击事件
        $('#filterButton').click(function() {
            const caregiver = $('#caregiver').val().toLowerCase();
            
            // 筛选表格行
            let visibleCount = 0;
            $('.caregiver-row').each(function() {
                const rowCaregiver = $(this).data('caregiver').toString().toLowerCase();
                const rowId = $(this).find('td:eq(0)').text().toLowerCase();
                
                const caregiverMatch = !caregiver || rowCaregiver.includes(caregiver) || rowId.includes(caregiver);
                
                if (caregiverMatch) {
                    $(this).show();
                    visibleCount++;
                } else {
                    $(this).hide();
                }
            });
            
            // 更新显示计数
            $('#visibleCount').text(visibleCount);
        });
        
        // 查看技能按钮点击事件
        $(document).on('click', '.view-skills-btn', function() {
            const id = $(this).data('id');
            const skills = $(this).data('skills');
            
            // 显示数据
            try {
                let formattedData = skills;
                if (typeof skills === 'string') {
                    formattedData = JSON.parse(skills);
                }
                $('#skillsContent').text(JSON.stringify(formattedData, null, 2));
            } catch (e) {
                $('#skillsContent').text(skills);
            }
            
            // 显示模态框
            const modal = new bootstrap.Modal(document.getElementById('skillsModal'));
            modal.show();
        });
        
        // 查看排班按钮点击事件
        $(document).on('click', '.view-schedule-btn', function() {
            const id = $(this).data('id');
            const caregiver = $(this).data('caregiver');
            
            // 跳转到护理员排班表页面
            window.open(`/page/shift_assignments_page?caregiver=${caregiver}`, '_blank');
        });
        
        // 导出到Excel按钮点击事件
        $('#exportButton').click(function() {
            // 获取可见行
            const visibleRows = $('.caregiver-row:visible');
            
            // 如果没有可见行，则提示
            if (visibleRows.length === 0) {
                alert('没有可导出的数据');
                return;
            }
            
            // 创建CSV内容
            let csvContent = 'ID,护理员\n';
            
            visibleRows.each(function() {
                const id = $(this).find('td:eq(0)').text();
                const caregiver = $(this).find('td:eq(1)').text();
                
                csvContent += `${id},${caregiver}\n`;
            });
            
            // 创建下载链接
            const encodedUri = encodeURI('data:text/csv;charset=utf-8,' + csvContent);
            const link = document.createElement('a');
            link.setAttribute('href', encodedUri);
            link.setAttribute('download', '护理员信息.csv');
            document.body.appendChild(link);
            
            // 点击下载链接
            link.click();
            
            // 移除下载链接
            document.body.removeChild(link);
        });
    });
</script>
{% endblock %}
